package org.light.simpleauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;

public class LoginUser extends Verb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;
	}
	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}
	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}
	
	public LoginUser(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("LoginUser");
		this.setVerbName("Login"+StringUtil.capFirst(this.domain.getStandardName()));
		this.setLabel("登录");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("LoginUser");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("processRequest");
			method.setReturnType(new Type("void"));
			method.setThrowException(true);
			List<String> list = new ArrayList<String>();
			list.add("ServletException");
			list.add("IOException");
			method.setIsprotected(true);
			method.setOtherExceptions(list);
			method.addSignature(new Signature(1,"request",new Type("HttpServletRequest","javax.servlet.http")));
			method.addSignature(new Signature(2,"response",new Type("HttpServletResponse","javax.servlet.http")));
			method.addAdditionalImport("java.io.IOException");
			method.addAdditionalImport("java.io.PrintWriter");
			method.addAdditionalImport("javax.servlet.ServletException");
			method.addAdditionalImport("javax.servlet.http.HttpServlet");
			method.addAdditionalImport("javax.servlet.http.HttpServletRequest");
			method.addAdditionalImport("javax.servlet.http.HttpServletResponse");
			method.addAdditionalImport("java.util.Map");
			method.addAdditionalImport("java.util.TreeMap");
			method.addAdditionalImport("net.sf.json.JSONObject");
			method.addAdditionalImport("javax.servlet.http.HttpSession");
			method.addAdditionalImport("java.io.BufferedReader");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+".utils.PasswordUtil");
			method.addAdditionalImport(this.domain.getPackageToken()+".utils.AppTokenManager");			
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceimplSuffix()+"."+this.domain.getStandardName()+"ServiceImpl");
			
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,2,"response.setContentType(\"application/json;charset=UTF-8\");"));
			sList.add(new Statement(2000L,2,"Map<String,Object> result = new TreeMap<String,Object>();"));
			sList.add(new Statement(3000L,2,"PrintWriter out = response.getWriter();"));
			sList.add(new Statement(4000L,2,"try {"));
			sList.add(new Statement(5000L,3,"StringBuilder jsonBuilder = new StringBuilder();"));
			sList.add(new Statement(6000L,3,"BufferedReader reader = request.getReader();"));
			sList.add(new Statement(7000L,3,"String line;"));
			sList.add(new Statement(8000L,3,"while ((line = reader.readLine()) != null) {"));
			sList.add(new Statement(9000L,4,"jsonBuilder.append(line);"));
			sList.add(new Statement(10000L,3,"}"));
			sList.add(new Statement(11000L,3,"String jsonString = jsonBuilder.toString();"));
			sList.add(new Statement(12000L,3,"JSONObject jsonReq = JSONObject.fromObject(jsonString);"));
			sList.add(new Statement(13000L,0,""));
			sList.add(new Statement(14000L,3,"String "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+" = jsonReq.getString(\""+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+"\");"));
			sList.add(new Statement(15000L,3,"String "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+" = jsonReq.getString(\""+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+"\");"));
			sList.add(new Statement(16000L,0,""));
			sList.add(new Statement(18000L,3,this.domain.getCapFirstDomainName()+"Service service = new "+this.domain.getCapFirstDomainName()+"ServiceImpl();"));
			FindUserShadow findShadow = new FindUserShadow(this.domain);
			sList.add(new Statement(19000L,3,this.domain.getCapFirstDomainNameWithSuffix()+" dbUser = service."+StringUtil.lowerFirst(findShadow.getVerbName())+"("+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+");"));
			sList.add(new Statement(20000L,3,"String salt = dbUser.get"+this.domain.findFieldByFieldName("salt").getCapFirstFieldName()+"();"));
			sList.add(new Statement(21000L,3,"String dbPassword = dbUser.get"+this.domain.findFieldByFieldName("password").getCapFirstFieldName()+"();"));
			sList.add(new Statement(22000L,3,"boolean loginSuccess = PasswordUtil.verifyPassword("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+",salt,dbPassword);"));
			sList.add(new Statement(23000L,3,"if (loginSuccess) {"));
			sList.add(new Statement(24000L,4,"HttpSession session = request.getSession();"));
			sList.add(new Statement(25000L,4,"String token = AppTokenManager.loginUser("+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+");"));
			FindByName findUser = new FindByName(this.domain);
			sList.add(new Statement(26000L,4,this.domain.getCapFirstDomainNameWithSuffix() + " "+this.domain.getLowerFirstDomainName()+" = service."+StringUtil.lowerFirst(findUser.getVerbName())+"("+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+");"));
			sList.add(new Statement(27000L,4,"session.setAttribute(\"user\", "+this.domain.getLowerFirstDomainName()+");"));
			sList.add(new Statement(28000L,4,"result.put(\"success\",loginSuccess);"));
			sList.add(new Statement(29000L,4,"result.put(\"token\",token);"));
			sList.add(new Statement(30000L,4,"result.put(\"data\",null);"));
			sList.add(new Statement(31000L,4,"out.print(JSONObject.fromObject(result).toString());"));
			sList.add(new Statement(32000L,4,"return;"));
			sList.add(new Statement(33000L,3,"} else {"));
			sList.add(new Statement(34000L,4,"result.put(\"success\",false);"));
			sList.add(new Statement(35000L,4,"result.put(\"data\",null);"));
			sList.add(new Statement(36000L,3,"}"));
			sList.add(new Statement(37000L,3,"out.print(JSONObject.fromObject(result).toString());"));
			sList.add(new Statement(38000L,2,"} catch (Exception e){"));
			sList.add(new Statement(39000L,3,"e.printStackTrace();"));
			sList.add(new Statement(40000L,3,"result.put(\"success\",false);"));
			sList.add(new Statement(41000L,3,"result.put(\"reason\",e.getMessage());"));
			sList.add(new Statement(42000L,3,"out.print(JSONObject.fromObject(result).toString());"));
			sList.add(new Statement(43000L,2,"} finally {"));
			sList.add(new Statement(44000L,3,"out.close();"));
			sList.add(new Statement(45000L,2,"}"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}	
	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;		
	}
	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyServiceImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyDaoMethodDefinition() throws Exception {
		return null;
	}
}
